/*	
	SPHEAL CLICKER
		Inspired by Cookie Clicker, Pokémon, and how fucking adorable Spheal are
		All Characters (Spheal, Sealeo, Walrein, etc.), Symbols (Stone Badge, Knuckle Badge, etc.), etc. belong to Nintendo, Game Freak, Creatures Inc., or their respective owners.
		ver 0.5
*/

/* VARIABLE DECLARATION */
	/* CLICKING */
	var clicks = 0;
	var clicks_all_time = 0;
	var cps = 0;
		/* AUTOCLICKERS */
		var autoclicking = false;
		var autoclickers = [
			{"type" 		: "Stone Badge",
			"fn"			: "clicker0",
			"cps"			: 0.5, "cps_modifier" : 1,
			"cost"			: 15,
			"basecost"		: 15,
			"amuletcoin"	: false,
			"quantity"		: 0,
			"appended"		: false},
			{"type" 		: "Knuckle Badge",
			"fn"			: "clicker1",
			"cps"			: 2, "cps_modifier" : 1,
			"cost"			: 150,
			"basecost"		: 150,
			"amuletcoin"	: false,
			"quantity"		: 0,
			"appended"		: false},
			{"type" 		: "Dynamo Badge",
			"fn"			: "clicker2",
			"cps"			: 10, "cps_modifier" : 1,
			"cost"			: 2500,
			"basecost"		: 2500,
			"amuletcoin"	: false,
			"quantity"		: 0,
			"appended"		: false},
			{"type" 		: "Heat Badge",
			"fn"			: "clicker3",
			"cps"			: 40, "cps_modifier" : 1,
			"cost"			: 10000,
			"basecost"		: 10000,
			"amuletcoin"	: false,
			"quantity"		: 0,
			"appended"		: false},
			{"type" 		: "Balance Badge",
			"fn"			: "clicker4",
			"cps"			: 100, "cps_modifier" : 1,
			"cost"			: 110000,
			"basecost"		: 110000,
			"amuletcoin"	: false,
			"quantity"		: 0,
			"appended"		: false},
			{"type" 		: "Feather Badge",
			"fn"			: "clicker5",
			"cps"			: 400, "cps_modifier" : 1,
			"cost"			: 320000,
			"basecost"		: 320000,
			"amuletcoin"	: false,
			"quantity"		: 0,
			"appended"		: false},
			{"type" 		: "Mind Badge",
			"fn"			: "clicker6",
			"cps"			: 1000, "cps_modifier" : 1,
			"cost"			: 600000,
			"basecost"		: 600000,
			"amuletcoin"	: false,
			"quantity"		: 0,
			"appended"		: false},
			{"type" 		: "Rain Badge",
			"fn"			: "clicker7",
			"cps"			: 5000, "cps_modifier" : 1,
			"cost"			: 1200000,
			"basecost"		: 1200000,
			"amuletcoin"	: false,
			"quantity"		: 0,
			"appended"		: false},
			{"type" 		: "Frontier Knowledge Symbol",
			"fn"			: "clicker8",
			"cps"			: 10000, "cps_modifier" : 1,
			"cost"			: 5000000,
			"basecost"		: 5000000,
			"amuletcoin"	: false,
			"quantity"		: 0,
			"appended"		: false},
			{"type" 		: "Frontier Luck Symbol",
			"fn"			: "clickerq",
			"cps"			: 35000, "cps_modifier" : 1,
			"cost"			: 7200000,
			"basecost"		: 7200000,
			"amuletcoin"	: false,
			"quantity"		: 0,
			"appended"		: false},
			{"type" 		: "Frontier Spirits Symbol",
			"fn"			: "clickerq",
			"cps"			: 50000, "cps_modifier" : 1,
			"cost"			: 9000000,
			"basecost"		: 9000000,
			"amuletcoin"	: false,
			"quantity"		: 0,
			"appended"		: false},
			{"type" 		: "Frontier Brave Symbol",
			"fn"			: "clickerq",
			"cps"			: 70000, "cps_modifier" : 1,
			"cost"			: 13000000,
			"basecost"		: 13000000,
			"amuletcoin"	: false,
			"quantity"		: 0,
			"appended"		: false},
			{"type" 		: "Frontier Tactics Symbol",
			"fn"			: "clickerq",
			"cps"			: 90000, "cps_modifier" : 1,
			"cost"			: 20000000,
			"basecost"		: 20000000,
			"amuletcoin"	: false,
			"quantity"		: 0,
			"appended"		: false},
			{"type" 		: "Frontier Guts Symbol",
			"fn"			: "clickerq",
			"cps"			: 120000, "cps_modifier" : 1,
			"cost"			: 24000000,
			"basecost"		: 24000000,
			"amuletcoin"	: false,
			"quantity"		: 0,
			"appended"		: false},
			{"type" 		: "Frontier Ability Symbol",
			"fn"			: "clickerq",
			"cps"			: 160000, "cps_modifier" : 1,
			"cost"			: 30000000,
			"basecost"		: 30000000,
			"amuletcoin"	: false,
			"quantity"		: 0,
			"appended"		: false}
		]
		
	/* PKMN */
	var pkmn = [];
	var caught = 0;
	var active;
	var withdrawn = false;
	var total_spheal = 0;
	
		/* SPAWN */
		var pkmncounter = 0;
		var pkmnclicklimit = 0;
		var pkmnlevel = Math.round(Math.random() * (32 - 1) + 1);
		var pkmncounteron = false;
		var pkmnspawntime = Math.random() * (180000 - 240000) + 240000;
		var capturedecrease = 0;		
		var shinychance = 0;
		var swimspawntime = Math.random() * (120000 - 300000) + 300000;
		var clicked_swim_spheal = 0;
	
	/* ITEMS */
	var inventory = [
		{"item" 	: "Rare Candy",
		"fn"		: "rarecandy",
		"quantity"	: 0},
		{"item" 	: "Amulet Coin",
		"fn"		: "amuletcoin",
		"quantity"	: 0},
		{"item" 	: "Poké Ball",
		"fn"		: "pokeball",
		"quantity"	: 0},
		{"item" 	: "Great Ball",
		"fn"		: "greatball",
		"quantity"	: 0},
		{"item" 	: "Ultra Ball",
		"fn"		: "ultraball",
		"quantity"	: 0},
		{"item" 	: "Master Ball",
		"fn"		: "masterball",
		"quantity"	: 0},
		{"item" 	: "Shiny Charm",
		"fn"		: "shinycharm",
		"quantity"	: 0}
	]
	var masterballused = false;

	var berries = [
		{"name"		: "Razz",
		"flavour"	: "spicy",
		"quantity"	: 0},
		{"name"		: "Bluk",
		"flavour"	: "dry",
		"quantity"	: 0},
		{"name"		: "Nanab",
		"flavour"	: "sweet",
		"quantity"	: 0},
		{"name"		: "Wepear",
		"flavour"	: "bitter",
		"quantity"	: 0},
		{"name"		: "Pinap",
		"flavour"	: "sour",
		"quantity"	: 0}
	]
	
		/* SPAWN */
		var itemcounter = 0;
		var itemspawntime = Math.random() * (120000 - 300000) + 300000;

	/* ACHIEVEMENTS */
	var achievements = [
		{"title" 	: "Spheal Smile",
		"desc"		: "Gather <strong>1</strong> Spheal Smile.",
		"achieved"	: false,
		"achieveid"	: 0, "rank"		: 1, "posx"		: 2, "posy"		: 1},

		{"title" 	: "Spheal Whisperer",
		"desc"		: "Gather <strong>10,000</strong> Spheal Smiles.",
		"achieved"	: false,
		"achieveid"	: 1, "rank"		: 2, "posx"		: 2, "posy"		: 1},

		{"title" 	: "Spheal Smile Expert",
		"desc"		: "Gather <strong>1,000,000</strong> Spheal Smiles.",
		"achieved"	: false,
		"achieveid"	: 2, "rank"		: 3, "posx"		: 2, "posy"		: 1},

		{"title" 	: "Champion of the Spheal Smiles",
		"desc"		: "Gather <strong>1,000,000,000</strong> Spheal Smiles.",
		"achieved"	: false,
		"achieveid"	: 3, "rank"		: 4, "posx"		: 2, "posy"		: 1},

		{"title" 	: "Badge get!",
		"desc"		: "Purchase <strong>1</strong> unique Badge.",
		"achieved"	: false,
		"achieveid"	: 6, "rank"		: 1, "posx"		: 3, "posy"		: 1},

		{"title" 	: "Badge Collector",
		"desc"		: "Purchase <strong>3</strong> unique Badges.",
		"achieved"	: false,
		"achieveid"	: 7, "rank"		: 2, "posx"		: 3, "posy"		: 1},

		{"title" 	: "Badge Enthusiast",
		"desc"		: "Purchase <strong>5</strong> unique Badges.",
		"achieved"	: false,
		"achieveid"	: 8, "rank"		: 3, "posx"		: 3, "posy"		: 1},

		{"title" 	: "Gym League Champion",
		"desc"		: "Purchase <strong>8</strong> unique Badges.",
		"achieved"	: false,
		"achieveid"	: 9, "rank"		: 4, "posx"		: 3, "posy"		: 1},

		{"title" 	: "The Rocky Road of Success",
		"desc"		: "Purchase <strong>10</strong> Stone Badges.",
		"achieved"	: false,
		"achieveid"	: 24, "rank"	: 2, "posx"		: 1, "posy"		: 5},

		{"title" 	: "Taking down Tate", 
		"desc"		: "Purchase <strong>10</strong> Mind Badges.",
		"achieved"	: false,
		"achieveid"	: 36, "rank"	: 2, "posx"		: 7, "posy"		: 5},

		{"title" 	: "Roxanne's Rival",
		"desc"		: "Purchase <strong>100</strong> Stone Badges.",
		"achieved"	: false,
		"achieveid"	: 25, "rank"	: 5, "posx"		: 1, "posy"		: 5},

		{"title" 	: "Cute Power",
		"desc"		: "Train a Spheal to Level 100 without evolving it.",
		"achieved"	: false,
		"achieveid"	: 10, "rank"	: 1, "posx"		: 4, "posy"		: 1},

		{"title" 	: "No added sugar",
		"desc"		: "Train a Spheal to Level 100 without evolving it or using a Rare Candy.",
		"achieved"	: false,
		"achieveid"	: 11, "rank"	: 2, "posx"		: 5, "posy"		: 1},

		{"title" 	: "Shiny Power",
		"desc"		: "Train a Shiny Spheal to Level 100 without evolving it.",
		"achieved"	: false,
		"achieveid"	: 12, "rank"	: 3, "posx"		: 6, "posy"		: 1},

		{"title" 	: "Shiny Charmer",
		"desc"		: "Capture <strong>1</strong> Shiny Spheal.",
		"achieved"	: false,
		"achieveid"	: 14, "rank"	: 4, "posx"		: 1, "posy"		: 2},

		{"title" 	: "Ooh, pretty!",
		"desc"		: "Capture <strong>10</strong> Shiny Spheal.",
		"achieved"	: false,
		"achieveid"	: 15, "rank"	: 5, "posx"		: 8, "posy"		: 1},

		{"title" 	: "Oy vey!",
		"desc"		: "Purchase a discounted Badge.",
		"achieved"	: false,
		"achieveid"	: 22, "rank"	: 5, "posx"		: 1, "posy"		: 2},

		{"title" 	: "Masterful",
		"desc"		: "Capture a Spheal using a Master Ball.",
		"achieved"	: false,
		"achieveid"	: 16, "rank"	: 5, "posx"		: 2, "posy"		: 2},

		{"title" 	: "Spheal Friend",
		"desc"		: "Capture <strong>1</strong> wild Spheal.",
		"achieved"	: false,
		"achieveid"	: 17, "rank"	: 3, "posx"		: 3, "posy"		: 2},

		{"title" 	: "Mayor of the Spheal",
		"desc"		: "Capture <strong>10</strong> wild Spheal.",
		"achieved"	: false,
		"achieveid"	: 18, "rank"	: 4, "posx"		: 3, "posy"		: 2},

		{"title" 	: "Benevolent Overseer of the Spheal Community",
		"desc"		: "Capture <strong>100</strong> wild Spheal.",
		"achieved"	: false,
		"achieveid"	: 19, "rank"	: 5, "posx"		: 3, "posy"		: 2},

		{"title" 	: "Sugar Rush",
		"desc"		: "Raise your Pokémon's Level using a Rare Candy.",
		"achieved"	: false,
		"achieveid"	: 20, "rank"	: 4, "posx"		: 4, "posy"		: 2},

		{"title" 	: "Sugar Crash",
		"desc"		: "Raise your Pokémon to <strong>Level 100</strong> using a Rare Candy.",
		"achieved"	: false,
		"achieveid"	: 21, "rank"	: 5, "posx"		: 4, "posy"		: 2},

		{"title" 	: "Deep-sea Diver",
		"desc"		: "Befriend a Swimming Spheal.",
		"achieved"	: false,
		"achieveid"	: 23, "rank"	: 5, "posx"		: 5, "posy"		: 2},

		{"title" 	: "Satanic Spheal",
		"desc"		: "Gather <strong>666,666,666</strong> Spheal Smiles.",
		"achieved"	: false,
		"achieveid"	: 5, "rank"		: 5, "posx"		: 6, "posy"		: 2},
		
		{"title" 	: "*cracks knuckles*",
		"desc"		: "Purchase <strong>10</strong> Knuckle Badges.",
		"achieved"	: false,
		"achieveid"	: 26, "rank"	: 2, "posx"		: 2, "posy"		: 5},

		{"title" 	: "Beat up Brawly",
		"desc"		: "Purchase <strong>100</strong> Knuckle Badges.",
		"achieved"	: false,
		"achieveid"	: 27, "rank"	: 5, "posx"		: 2, "posy"		: 5},
		
		{"title" 	: "Shocking Victory",
		"desc"		: "Purchase <strong>10</strong> Dynamo Badges.",
		"achieved"	: false,
		"achieveid"	: 28, "rank"	: 2, "posx"		: 3, "posy"		: 5},

		{"title" 	: "New Mauville's Nightmare",
		"desc"		: "Purchase <strong>100</strong> Dynamo Badges.",
		"achieved"	: false,
		"achieveid"	: 29, "rank"	: 5, "posx"		: 3, "posy"		: 5},
		
		{"title" 	: "Smoked",
		"desc"		: "Purchase <strong>10</strong> Heat Badges.",
		"achieved"	: false,
		"achieveid"	: 30, "rank"	: 2, "posx"		: 4, "posy"		: 5},

		{"title" 	: "Flannery's Fiery Rage",
		"desc"		: "Purchase <strong>100</strong> Heat Badges.",
		"achieved"	: false,
		"achieveid"	: 31, "rank"	: 5, "posx"		: 4, "posy"		: 5},
		
		{"title" 	: "Balance Badge Collector",
		"desc"		: "Purchase <strong>10</strong> Balance Badges.",
		"achieved"	: false,
		"achieveid"	: 32, "rank"	: 2, "posx"		: 5, "posy"		: 5},
	
		{"title" 	: "Norman's not happy",
		"desc"		: "Purchase <strong>100</strong> Balance Badges.",
		"achieved"	: false,
		"achieveid"	: 33, "rank"	: 5, "posx"		: 5, "posy"		: 5},
		
		{"title" 	: "The Winds of Success",
		"desc"		: "Purchase <strong>10</strong> Feather Badges.",
		"achieved"	: false,
		"achieveid"	: 34, "rank"	: 2, "posx"		: 6, "posy"		: 5},
		
		{"title" 	: "The Wind Waker",
		"desc"		: "Purchase <strong>100</strong> Feather Badges.",
		"achieved"	: false,
		"achieveid"	: 35, "rank"	: 5, "posx"		: 6, "posy"		: 5},

		{"title" 	: "Liza's Lament",
		"desc"		: "Purchase <strong>100</strong> Mind Badges.",
		"achieved"	: false,
		"achieveid"	: 37, "rank"	: 5, "posx"		: 8, "posy"		: 5},

		{"title" 	: "A Sculpture of Success", 
		"desc"		: "Purchase <strong>10</strong> Rain Badges.",
		"achieved"	: false,
		"achieveid"	: 38, "rank"	: 2, "posx"		: 1, "posy"		: 6},

		{"title" 	: "Winning against Wallace!", 
		"desc"		: "Purchase <strong>100</strong> Rain Badges.",
		"achieved"	: false,
		"achieveid"	: 39, "rank"	: 5, "posx"		: 2, "posy"		: 6},

		{"title" 	: "Taking on the Battle Frontier",
		"desc"		: "Purchase <strong>1</strong> Frontier Symbol.",
		"achieved"	: false,
		"achieveid"	: 40, "rank"	: 5, "posx"		: 7, "posy"		: 2},

		{"title" 	: "Head-to-Head with the Factory Head", 
		"desc"		: "Purchase <strong>10</strong> Frontier Knowledge Symbols.",
		"achieved"	: false,
		"achieveid"	: 41, "rank"	: 2, "posx"		: 3, "posy"		: 6},

		{"title" 	: "No Victory for Noland!", 
		"desc"		: "Purchase <strong>100</strong> Frontier Knowledge Symbols.",
		"achieved"	: false,
		"achieveid"	: 42, "rank"	: 5, "posx"		: 3, "posy"		: 6},

		{"title" 	: "The Very Essence of Happiness", 
		"desc"		: "Garner <strong>1,000,000,000,000</strong> Spheal Smiles.",
		"achieved"	: false,
		"achieveid"	: 4, "rank"	: 5, "posx"		: 2, "posy"		: 1},

		{"title" 	: "Shiny Power, with no added sugar",
		"desc"		: "Train a Shiny Spheal to Level 100 without evolving it or using a Rare Candy.",
		"achieved"	: false,
		"achieveid"	: 13, "rank" : 3, "posx" : 7, "posy" : 1},

		{"title" 	: "Lucky Seven", 
		"desc"		: "Purchase <strong>7</strong> Frontier Luck Symbols.",
		"achieved"	: false,
		"achieveid"	: 43, "rank"	: 2, "posx"		: 4, "posy"		: 6},

		{"title" 	: "Dethroning the Pike Queen", 
		"desc"		: "Purchase <strong>100</strong> Frontier Luck Symbols.",
		"achieved"	: false,
		"achieveid"	: 44, "rank"	: 5, "posx"		: 4, "posy"		: 6},

		{"title" 	: "Spiritual Cleansing", 
		"desc"		: "Purchase <strong>10</strong> Frontier Spirits Symbols.",
		"achieved"	: false,
		"achieveid"	: 45, "rank"	: 2, "posx"		: 5, "posy"		: 6},

		{"title" 	: "Palace Maven May Cry", 
		"desc"		: "Purchase <strong>100</strong> Frontier Spirits Symbols.",
		"achieved"	: false,
		"achieveid"	: 46, "rank"	: 5, "posx"		: 5, "posy"		: 6},

		{"title" 	: "Braving Brandon", 
		"desc"		: "Purchase <strong>10</strong> Frontier Brave Symbols.",
		"achieved"	: false,
		"achieveid"	: 47, "rank"	: 2, "posx"		: 6, "posy"		: 6},

		{"title" 	: "Brandon's Breakdown", 
		"desc"		: "Purchase <strong>100</strong> Frontier Brave Symbols.",
		"achieved"	: false,
		"achieveid"	: 48, "rank"	: 5, "posx"		: 6, "posy"		: 6},

		{"title" 	: "Fabulous", 
		"desc"		: "Purchase <strong>10</strong> Frontier Tactics Symbols.",
		"achieved"	: false,
		"achieveid"	: 49, "rank"	: 2, "posx"		: 7, "posy"		: 6},

		{"title" 	: "What? It means happy...", 
		"desc"		: "Purchase <strong>100</strong> Frontier Tactics Symbols.",
		"achieved"	: false,
		"achieveid"	: 50, "rank"	: 5, "posx"		: 7, "posy"		: 6},

		{"title" 	: "Taking down the Arena Tycoon", 
		"desc"		: "Purchase <strong>10</strong> Frontier Guts Symbols.",
		"achieved"	: false,
		"achieveid"	: 51, "rank"	: 2, "posx"		: 8, "posy"		: 6},

		{"title" 	: "Grounding Greta", 
		"desc"		: "Purchase <strong>100</strong> Frontier Guts Symbols.",
		"achieved"	: false,
		"achieveid"	: 52, "rank"	: 5, "posx"		: 8, "posy"		: 6},

		{"title" 	: "Battling the Battle Tower", 
		"desc"		: "Purchase <strong>10</strong> Frontier Ability Symbols.",
		"achieved"	: false,
		"achieveid"	: 53, "rank"	: 2, "posx"		: 1, "posy"		: 7},

		{"title" 	: "Anabel's Ability (to lose)", 
		"desc"		: "Purchase <strong>100</strong> Frontier Ability Symbols.",
		"achieved"	: false,
		"achieveid"	: 54, "rank"	: 5, "posx"		: 1, "posy"		: 7}
	]

	var unlocked_achievements = 0;
	
	/* MISC */
	var autosave = true;
	var notifs = true;

/* CLICKING */
	/* CLICK */
	function clicked(add) {
		/* BASIC CLICK */
		add += pkmnmodifier;
		clicks += add;
		clicks_all_time += add;
		appendclickers();
		recalcstats();
		updateTitle();
		
		/* EXPERIENCE POINTS */
		pkmn[active].exp += add;
		if (pkmn[active].exp >= 4 * Math.pow(pkmn[active].level,3) / 5){
			levelUp();
		}
		
		/* ACHIEVEMENT CHECKS */
		if (clicks >= 1){achieve(0);}
		if (clicks >= 10000){achieve(1);}
		if (clicks >= 1000000){achieve(2);}
		if (clicks >= 666666666){achieve(24);}
		if (clicks >= 1000000000){achieve(3);}
		if (clicks >= 1000000000000){achieve(41);}
	}

	/* AUTOCLICKERS */
		window.setInterval(function(){for (var clicker=0;clicker<autoclickers.length;clicker++){if (autoclickers[clicker].quantity > 0){clickingauto = true;break;}}},1000) // Confirm that it should be automatically clicking
		window.setInterval(function(){if(clickingauto){clicked(cps);}},1000)	// Click automatically every second

		function sellclicker(clicker, amount){	
			if (amount == 'all' || autoclickers[clicker].quantity - amount < 0){
				amount = autoclickers[clicker].quantity;
			}
			var overallcost = autoclickers[clicker].basecost;
			for (var i = 0; i < amount; i++){
				overallcost += Math.round(autoclickers[clicker].basecost * Math.pow(1.15,i));
			}	
			if (autoclickers[clicker].quantity > 0){
				if (amount == 1){
					popUp('出售 ' + amount + ' ' + cnItem(autoclickers[clicker].type) + '!');
				} else {
					popUp('出售 ' + amount + ' ' + cnItem(autoclickers[clicker].type) + '!');
				}
				autoclickers[clicker].quantity -= amount;
				clicks += 2 * overallcost / 3;
				recalcclicker(clicker);
			}
			recalccps();
		}
		function recalcclicker(clicker){
			autoclickers[clicker].cost = Math.round(autoclickers[clicker].basecost * Math.pow(1.15,autoclickers[clicker].quantity));
			document.getElementById('cps' + clicker).innerHTML = '+' + numberWithCommas(roundTo(2,autoclickers[clicker].cps)) + ' 每秒';
			document.getElementById("bought" + clicker).innerHTML = autoclickers[clicker].quantity;
			document.getElementById("cost" + clicker).innerHTML = numberWithCommas(autoclickers[clicker].cost);	
			recalcstats();
		}

		function appendclickers(){
			if (clicks >= 1 && !autoclickers[0].appended){appendclicker(0);}	
			if (autoclickers[0].quantity >= 1 && !autoclickers[1].appended){appendclicker(1);}
			if (autoclickers[1].quantity >= 5 && !autoclickers[2].appended){appendclicker(2);}
			if (autoclickers[2].quantity >= 5 && !autoclickers[3].appended){appendclicker(3);}
			if (autoclickers[3].quantity >= 5 && !autoclickers[4].appended){appendclicker(4);}
			if (autoclickers[4].quantity >= 5 && !autoclickers[5].appended){appendclicker(5);}
			if (autoclickers[5].quantity >= 5 && !autoclickers[6].appended){appendclicker(6);}
			if (autoclickers[6].quantity >= 5 && !autoclickers[7].appended){appendclicker(7);}
			if (autoclickers[7].quantity >= 5 && !autoclickers[8].appended){appendclicker(8);} 
			if (autoclickers[8].quantity >= 5 && !autoclickers[9].appended){appendclicker(9);}
			if (autoclickers[9].quantity >= 5 && !autoclickers[10].appended){appendclicker(10);}
			if (autoclickers[10].quantity >= 5 && !autoclickers[11].appended){appendclicker(11);}
			if (autoclickers[11].quantity >= 5 && !autoclickers[12].appended){appendclicker(12);}
			if (autoclickers[12].quantity >= 5 && !autoclickers[13].appended){appendclicker(13);}
			if (autoclickers[13].quantity >= 5 && !autoclickers[14].appended){appendclicker(14);}
		}
		
		function buyclicker(clicker, amount){ // Buy autoclicker
			var overallcost = autoclickers[clicker].cost;
			if(amount>1){
				for (var i = 0; i < amount; i++){
					overallcost += Math.round(autoclickers[clicker].basecost * Math.pow(1.15,i + autoclickers[clicker].quantity));
				}
			}
			if (clicks >= overallcost){
				clicks -= overallcost;
				autoclickers[clicker].quantity += amount;
				recalcclicker(clicker);
				if (amount == 1){
					popUp('已购买 ' + amount + ' ' + cnItem(autoclickers[clicker].type) + '!');
				} else {
					popUp('已购买 ' + amount + ' ' + cnItem(autoclickers[clicker].type) + '!');
				}
				
				/* ACHIEVEMENT CHECKS */
				if(autoclickers[clicker].amuletcoin){achieve(16);autoclickers[clicker].amuletcoin = false;} // If an amulet coin was used
				achieve(4);
				if(autoclickers[0].quantity >= 10){achieve(8);} // Roxanne
				if(autoclickers[0].quantity >= 100){achieve(10);}
				
				if(autoclickers[1].quantity >= 10){achieve(25);} // Brawly
				if(autoclickers[1].quantity >= 100){achieve(26);}
				
				if(autoclickers[2].quantity >= 10){achieve(27);} // Wattson
				if(autoclickers[2].quantity >= 100){achieve(28);}
				
				if(autoclickers[3].quantity >= 10){achieve(29);} // Flannery
				if(autoclickers[3].quantity >= 100){achieve(30);}
				
				if(autoclickers[4].quantity >= 10){achieve(31);} // Norman
				if(autoclickers[4].quantity >= 100){achieve(32);}
				
				if(autoclickers[5].quantity >= 10){achieve(33);} // Winona
				if(autoclickers[5].quantity >= 100){achieve(34);}
				
				if(autoclickers[6].quantity >= 10){achieve(9);} // Tate
				if(autoclickers[6].quantity >= 100){achieve(35);} // Liza
				
				if(autoclickers[7].quantity >= 10){achieve(36);} // Juan
				if(autoclickers[7].quantity >= 100){achieve(37);} // Wallace
				
				if(autoclickers[8].quantity >= 10){achieve(39);} // Noland
				if(autoclickers[8].quantity >= 100){achieve(40);}
				
				if(autoclickers[9].quantity >= 7){achieve(43);} // Lucy
				if(autoclickers[9].quantity >= 100){achieve(44);}
				
				if(autoclickers[10].quantity >= 10){achieve(45);} // Spenser
				if(autoclickers[10].quantity >= 100){achieve(46);}
				
				if(autoclickers[11].quantity >= 10){achieve(47);} // Brandon
				if(autoclickers[11].quantity >= 100){achieve(48);}
				
				if(autoclickers[12].quantity >= 10){achieve(49);} // Tucker
				if(autoclickers[12].quantity >= 100){achieve(50);}
				
				if(autoclickers[13].quantity >= 10){achieve(51);} // Greta
				if(autoclickers[13].quantity >= 100){achieve(52);}
				
				if(autoclickers[14].quantity >= 10){achieve(53);} // Anabel
				if(autoclickers[14].quantity >= 100){achieve(54);}

				var unique_badges = 0; // Count the amount of unique Badges owned
				for (var clicker = 0; clicker < autoclickers.length; clicker++){
					if(autoclickers[clicker].quantity > 0){unique_badges++;}
				}

				if(unique_badges >= 3){achieve(5);}
				if(unique_badges >= 5){achieve(6);}
				if(unique_badges >= 8){achieve(7);}		
				
				var unique_symbols = 0; // Count the amount of unique Frontier Symbol owned
				for (var clicker = 8; clicker < autoclickers.length; clicker++){
					if(autoclickers[clicker].quantity > 0){unique_symbols++;}
				}

				if(unique_symbols >= 1){achieve(38);} // Frontier Symbol	
				appendclickers();
				recalccps();
			}
		}
		
		function appendclicker(clicker){
			var clickerdiv =
				'<div class="autoclicker"><span style="width: 100%;" onclick="buyclicker(' + clicker + ', 1)">' +
				'<img src="images/clickers/' + autoclickers[clicker].fn + '.png" />' +
				'</span><span id="autoclicker_info"><span style="width: 100%;" onclick="buyclicker(' + clicker + ', 1)">' +
				'<strong><span id="name' + clicker + '">' + cnItem(autoclickers[clicker].type) +
				'</span></strong> (<span id="bought' + clicker + '">' + numberWithCommas(autoclickers[clicker].quantity) + '</span>)' +
				' - <strong>价格:</strong> ' +
				'<span id="cost' + clicker + '">' + numberWithCommas(autoclickers[clicker].cost) + '</span>' +
				'</span><br/>' +
				'<span id="cps' + clicker + '">+' + numberWithCommas(roundTo(2,autoclickers[clicker].cps)) + ' 每秒</span>' +
				'<span id="autoclicker_options">' +
				'<a onclick="buyclicker(' + clicker + ',1)">购买 1</a>' +
				' | ' +
				'<a onclick="buyclicker(' + clicker + ',10)">购买 10</a>' +
				' | ' +
				'<a onclick="sellclicker(' + clicker + ',1)">出售 1</a>' +
				' | ' +
				'<a onclick="sellclicker(' + clicker + ',\'all\')">出售全部</a>' +
				'</span>' +
				'</span>' +
				'</div>';
			$(clickerdiv).hide().appendTo('.autoclicker_container').fadeIn(1000);
			autoclickers[clicker].appended = true;
		}		
		
/* POKéMON */
	/* INSTANTIATION */
	function pokemon(level, exp, species, nickname, id, shiny, rarecandies){
		this.level = level;
		this.exp = exp;
		this.species = species;
		this.nickname = nickname;
		this.id = id;
		this.shiny = shiny;
		this.rarecandies = rarecandies;
	}
		
	/* CAPTURE */
	function capturepkmn(lvl,shiny){
		caught++;
		if(withdrawn){document.getElementById('caught').innerHTML = numberWithCommas(caught-1);}else{document.getElementById('caught').innerHTML = numberWithCommas(caught);}
		pkmn[caught] = new pokemon(lvl,0,"Spheal","No Name", caught, shiny, 0);
		$('#boxlist').append("<span class='" + pkmn[caught].species + "icon' id='" + pkmn[caught].id + "' onclick='selectpkmn(this)'></span>");
		popUp('新捕获的 ' + cnItem(pkmn[caught].species) + ' 放进盒子里了!');
		calcmodifier();
		recalcstats();
		autoSave();
		
		/* ACHIEVEMENT CHECKS */
		if (!achievements[14].achieved){
			for (var i = 1; i < pkmn.length; i++){
				if(pkmn[i].shiny){
					achieve(14);
				}
			}
		}
		var shinies = 0;
		for (var i = 1; i < pkmn.length; i++){
			if (pkmn[i].shiny){
				shinies++;
				if (shinies >= 10){
					achieve(15);
				}
			}
		}
		if (pkmn.length > 2){
			achieve(18);
		}

		if (pkmn.length > 11){
			achieve(19);
		}

		if (pkmn.length > 101){
			achieve(20);
		}	
	}

	/* WITHDRAW */
		/* SELECET BOXED PKMN */
		function selectpkmn(obj){
			var id = parseFloat($(obj).attr('id'));
			document.getElementById('pkmnStats').innerHTML = cnItem(pkmn[id].nickname) + ' 的 ' + cnItem(pkmn[id].species) + '<br/>等级: ' + pkmn[id].level;
			boxOptionsShow(id);
		}
			
		/* SHOW BOX OPTIONS */
			function boxOptionsShow(obj) {
				document.getElementById('boxOptions').innerHTML = '<center><button onclick="withdrawpkmn('+ obj +')">出战</button><button onclick="boxOptionsHide()">取消</button></center>';
			}
				
		/* HIDE BOX OPTIONS */
			function boxOptionsHide() {
				document.getElementById('boxOptions').innerHTML = '';
				document.getElementById('pkmnStats').innerHTML = '';	
			}
	
		/* WITHDRAW SELECTED PKMN */
		function withdrawpkmn(id){
			if(withdrawn){
				depositpkmn();
			}
			active = pkmn[id].id;
			document.getElementById('caught').innerHTML = numberWithCommas(caught - 1);			
			document.getElementById('nametag').innerHTML = '<span ondblclick="addInput(this)" id="nickname">' + cnItem(pkmn[id].nickname) + '</span> 的 <span id="species">' + cnItem(pkmn[id].species) + '</span>';
			document.getElementById('pkmninfo').innerHTML = '等级 <span id="level">' + pkmn[id].level + '</span><br/><span id="evolve"></span><span id="rename"><button onclick="renamepkmn()">重命名</button></span><button onclick="depositpkmn(' + pkmn[id].id + ')">寄存</button>';
			if (pkmn[id].shiny){
				document.getElementById('pkmn').innerHTML = '<div id="shiny' + pkmn[id].species + '" onclick="clicked(1)"></div>';
			}else{
				document.getElementById('pkmn').innerHTML = '<div id="' + pkmn[id].species + '" onclick="clicked(1)"></div>';			
			}
			boxOptionsHide();
			document.getElementById(id).remove();
			popUp('Successfully withdrawn!');
			withdrawn = true;
			evolveCheck();
		}

	/* DEPOSIT */
	function depositpkmn(){
		$('#boxlist').append("<span class='" + pkmn[active].species + "icon' id='" + pkmn[active].id + "' onclick='selectpkmn(this)'></span>");
		withdrawn = false;
		document.getElementById('pkmn').innerHTML = '<div id="nopkmn"></div>';	
		document.getElementById('nametag').innerHTML = '从你的盒子里装备一只宝可梦!';
		document.getElementById('pkmninfo').innerHTML = '<br/>';
		document.getElementById('caught').innerHTML = numberWithCommas(caught);
		
		popUp('Successfully deposited!');
	}

	/* LEVEL UP */
	function levelUp(){
		if(pkmn[active].level < 100){
			/* ACHIEVEMENT CHECKS */
			if (pkmn[active].level == 99 && pkmn[active].species == 'Spheal'){
				achieve(11);
				if (pkmn[active].rarecandies == 0){
					achieve(12);
				}
			}
			pkmn[active].level++;
			document.getElementById('level').innerHTML = pkmn[active].level;
			popUp('升级了!');
			pkmn[active].exp = 0;
			calcmodifier();
			evolveCheck();	
		}
	}
	
	/* EVOLVE */
	function evolve(){
		popUp('Huh?! ' +  pkmn[active].nickname + ' is evolving!');
		if (pkmn[active].level >= 32 && pkmn[active].species == "Spheal"){
			pkmn[active].species = "Sealeo";
			document.getElementById('evolve').innerHTML = "<span id='evolvebutton'></span>"
		}else if(pkmn[active].level >= 44 && pkmn[active].species == "Sealeo"){
			pkmn[active].species = "Walrein";
			document.getElementById('evolve').innerHTML = "<span id='evolvebutton'></span>"
		}
		if (pkmn[active].shiny){
			document.getElementById('pkmn').innerHTML = '<div id="shiny' + pkmn[active].species + '" onclick="clicked(1)"></div>';
		}else{
			document.getElementById('pkmn').innerHTML = '<div id="' + pkmn[active].species + '" onclick="clicked(1)"></div>';			
		}	
		document.getElementById('nametag').innerHTML = '<span ondblclick="addInput(this)" id="nickname">' + cnItem(pkmn[active].nickname) + '</span> 的 <span id="species">' + cnItem(pkmn[active].species) + '</span>';
		popUp( cnItem(pkmn[active].nickname) + ' 进化成了 ' +  cnItem(pkmn[active].species) + '!');
		calcmodifier();
		autoSave();
	}
	
	function evolveCheck(){
		if (pkmn[active].level >= 32 && pkmn[active].species == "Spheal" || pkmn[active].level >= 44 && pkmn[active].species == "Sealeo"){
			document.getElementById('evolve').innerHTML = "<span id='evolvebutton'><button onclick='evolve()'>进化!</button><br/></span>"
		}	
	}
	
	/* RENAME */
		/* RENAME BUTTON */	
		function renamepkmn(){
			if (document.getElementById('nickname').getElementsByTagName('input').length > 0) return;
			
			var value = document.getElementById('nickname').innerHTML;
			document.getElementById('nickname').innerHTML = '';
			var input = document.createElement('input');
			input.setAttribute('type', 'text');
			input.setAttribute("maxlength", 250);
			input.setAttribute('value', value);
			input.setAttribute('onBlur', 'closeInput(this)');
			document.getElementById('nickname').appendChild(input);
			document.getElementById('rename').innerHTML = '<button onclick="closeInput(this)">保存</button>';
			input.focus();
		}
		
		/* CLOSE RENAME INPUT */	
		function closeInput(elm) {
			var span = elm.parentNode;
			var value = elm.value;
			span.removeChild(elm);
			span.innerHTML = value;
			pkmn[active].nickname = value;
			document.getElementById('rename').innerHTML = '<button onclick="renamepkmn()">重命名</button>';
		}	

	/* SPAWN */
	setTimeout(spawnpkmn, pkmnspawntime);

	function spawnpkmn() {
		var shiny = false;
		if (0.1 < shinychance){
			var y = roundTo(3,Math.random());
			if (y < 0.8){		
				shiny = true;
				shinychance = 0;
			}
		}
		
		var posx = (Math.random() * ($(document).width() - 96)).toFixed();
		var posy = (Math.random() * ($(document).height() - 96)).toFixed();

		$newdiv = $('<div/>')
			.attr('class', 'wildpkmn')
			.css({'left': posx + 'px','top': posy + 'px',})
			.appendTo('body')
			.fadeIn(5000);
			
		if (shiny) {
			popUp( "哇! 一个闪亮的海豹球出现了!");
			$('.wildpkmn').attr('id', 'wildshinyspheal');
		} else {
			popUp( "一只野生的海豹球出现了!");
			$('.wildpkmn').attr('id', 'wildspheal');
		}
		
		$('.wildpkmn')	
			.click(
				function () {
					if (!pkmncounteron){
						pkmnlevel = Math.round(Math.random() * (32 - 1) + 1);
						pkmnclicklimit = pkmnlevel * 4;		
						popUp( "点击 <strong>" + pkmnclicklimit + "</strong> 次去抓捕它!");
						pkmncounteron = true;
						if (capturedecrease > 0){
							if (masterballused){
								achieve(17);
								masterballused = false;
							}
							if ((pkmnclicklimit - Math.round(pkmnclicklimit * capturedecrease)) <= 0){
								pkmnclicklimit = 1;
								capturedecrease = 0;
							}
							pkmnclicklimit -= Math.round(pkmnclicklimit * capturedecrease);							
							popUp('所需的点击次数减少到 ' + pkmnclicklimit + '!');
							capturedecrease = 0;
						}
						$(this).append('<div id="counter">等级 '+ pkmnlevel +'<br/>'+ pkmncounter +' / '+ pkmnclicklimit +'</div>');
					}
					if (pkmncounteron){
						if(pkmncounter + 1 < pkmnclicklimit) {
							pkmncounter++;
							document.getElementById('counter').innerHTML = '等级 '+ pkmnlevel +'<br/>'+ pkmncounter +' / '+ pkmnclicklimit;
						} else {
							pkmncounter = 0;
							pkmnclicklimit = 0;
							pkmncounteron = false;
							capturepkmn(pkmnlevel,shiny);
							document.getElementById('counter').remove();
							pkmnspawntime = Math.random() * (180000 - 240000) + 240000;
							setTimeout(spawnpkmn, pkmnspawntime);
							$(this).remove();
						}
					}
				}
			)
			.delay(90000)
			.fadeOut(5000,
				function () {
					$(this).remove();
				}
			)
		;
		
	}

	/* SWIMMING SPHEAL */
	setTimeout(spawnswim, swimspawntime);
	
	function spawnswim() {

		var posx = (Math.random() * ($(document).width() - 140)).toFixed();
		var posy = (Math.random() * ($(document).height() - 140)).toFixed();
		$('body')
			.append('<div class="swimcont" style="left: '+posx+'px; top: '+posy+'px;"><img id="img"/></div>');

		$('.swimcont')
			.fadeIn(200,
				function () {
					$('#img').show().each(function() {
						this.offsetHeight;
					}).prop('src', 'images/swimspheal.gif');
				})
			.click(
				function () {
					$('#img').removeAttr('src', '');
					if (clicks > 1){
						var n = Math.min(roundTo(1,clicks * 0.15), roundTo(1,(cps + pkmnmodifier) * 1500));
					}else{
						var n = Math.random() * (1 - 100) + 100;
					}
					clicked_swim_spheal++;
					clicked(n);
					if (!achievements[23].achieved){
						achieve(23);
					}
					popUp('游泳海豹给了你 ' + numberWithCommas(roundTo(1,n)) + ' 微笑!');					
					swimspawntime = Math.random() * (120000 - 300000) + 300000;
                    setTimeout(spawnswim, swimspawntime);
					$(this).remove();
			})
			.delay(7000)
			.fadeOut(1,
				function () {
					$('#img').removeAttr('src', '');
					swimspawntime = Math.random() * (120000 - 300000) + 300000;
					setTimeout(spawnswim, swimspawntime);
					$(this).remove();
				}
			)
		;
    }

/* ITEMS */
	/* SPAWN */
	setTimeout(randomitem, itemspawntime);	
	
	function spawnitem(item) {

		var posx = (Math.random() * ($(document).width() - 96)).toFixed();
		var posy = (Math.random() * ($(document).height() - 96)).toFixed();
		var fn = inventory[item].fn;
		$newdiv = $('<div/>')
			.css({
				"left": posx + "px",
				"top": posy + "px",
				"background-image": "url('images/items/" + fn + ".png')",
			})
			.appendTo('body')
			.click(
				function () {
					popUp('Obtained one ' + inventory[item].item + '!');
					
					inventory[item].quantity++;
					$('#inventorylist').append("<span style='cursor: pointer;' onclick='use" + inventory[item].fn + "(this)'><img src='images/items/" + inventory[item].fn + ".png' style='height:24px;width:24px;'/></span>");	
					itemspawntime = Math.random() * (120000 - 300000) + 300000;
					setTimeout(randomitem, itemspawntime);	
					$(this).remove();
				}
			)
			.attr('id', 'item')
			.fadeIn(5000)
			.delay(60000)
			.fadeOut(5000,
				function () {
					itemspawntime = Math.random() * (120000 - 300000) + 300000;
					setTimeout(randomitem, itemspawntime);	
					$(this).remove();
				}
			)
		;
	}
	
	function randomitem(){
		var weighted_inventory = [
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
				2, 2, 2, 2, 2, 2, 2, 2,
				3, 3, 3, 3, 3, 3,
				4, 4, 4,
				5, 5,
				6];
		var x = Math.floor(Math.random() * weighted_inventory.length);
		spawnitem(weighted_inventory[x]);
	}	
	
	/* RARE CANDY */
	function userarecandy(obj){
		if(pkmn[active].level < 100){
			popUp('使用一个稀有糖果!');
			pkmn[active].rarecandies++;
			inventory[0].quantity -= 1;
			levelUp();
			
			/* ACHIEVEMENT CHECKS */
			achieve(21);
					
			if (pkmn[active].level == 100){
				achieve(22);
				if (pkmn[active].shiny){
					achieve(42);
				}
			}
			
			$(obj).remove();
		}
	}

	/* AMULET COIN */
	function useamuletcoin(obj){
		var clicker = Math.floor((Math.random() * autoclickers.length));
		if (autoclickers[clicker].appended){
			autoclickers[clicker].cost = Math.round(autoclickers[clicker].cost / 3 * 2);
			document.getElementById("cost" + clicker).innerHTML = numberWithCommas(autoclickers[clicker].cost);
			inventory[1].quantity -= 1;	
			autoclickers[clicker].amuletcoin = true;
			popUp('使用了护身符硬币!');
			popUp('你下一个 ' + cnItem(autoclickers[clicker].type) + ' 的成本稍微减少了!');
			$(obj).remove();
		} else {
			useamuletcoin(obj);
		}
	}
	
	/* BALL */
	function useball(id,name,amount){
		popUp('使用了一个 ' + cnItem(name) + '!');
		capturedecrease += amount;
		inventory[id].quantity -= 1;
		popUp('下一次捕获将减少点击次数!');
	}
	
		/* POKé BALL */
		function usepokeball(obj){
			$(obj).remove();
			useball(2,inventory[2].item,0.2);
		}
		
		/* GREAT BALL */
		function usegreatball(obj){
			$(obj).remove();
			useball(3,inventory[3].item,0.4);
		}
		
		/* ULTRA BALL */
		function useultraball(obj){
			$(obj).remove();
			useball(4,inventory[4].item,0.7);		
		}	
		
		/* MASTER BALL */
		function usemasterball(obj){
			popUp('使用了一个 ' + cnItem(inventory[5].item) + '!');
			$(obj).remove();
			capturedecrease += 1;
			inventory[5].quantity -= 1;
			popUp('下一次捕获只需点击一下!');
			masterballused = true;
		}

	/* SHINY CHARM */
	function useshinycharm(obj){
		popUp('使用了闪亮的魅力!');
		inventory[6].quantity -=1;	
		shinychance += roundTo(2,Math.random());
		popUp('什么都没有发生..?');
		$(obj).remove();
	}

/* ACHIEVEMENTS */

	/* ACHIEVING ACHIEVEMENT */
	function achieve(id){
		if(!achievements[id].achieved){
			achievements[id].achieved = true;
			popUp('<strong>成就解锁:</strong> ' + achievements[id].title);
			changeleft('achievements');
			var posx = -64 * (achievements[id].posx - 1);
			var posy = -64 * (achievements[id].posy - 1);
			var rank = achievements[id].rank;			
			var acvmtdiv =
				'<div class="achieve' + rank + '" id="'+id+'wrapper"><div class="achievement" id="achieve'+id+'" style="background-image:url(images/icons.png); background-position:' + posx + 'px ' + posy + 'px;" onmouseover="achievehover('+id+',true)" /></div>';
			$('#'+ id + 'wrapper').fadeOut(200, function(){
				document.getElementById(id + 'wrapper').innerHTML = acvmtdiv;
				document.getElementById(id + 'wrapper').className = 'achieve' + rank;
			}).fadeIn(100);
			unlocked_achievements++;
			recalcstats();
			autoSave();
		}
	}

	/* HOVERING OVER / OUT OF AN ACHIEVMENT */
	function achievehover(id,mousein){
		if(mousein){
			var n = '???';
			if (achievements[id].achieved){
				n = achievements[id].desc;
			}
			var acvmtdiv = '<span id="achievedesc"><i>' + cnItem(achievements[id].title) + '</i><br/>'+ n +'</span>';
			$('#achievehover').html(acvmtdiv);
			$(acvmtdiv).hide().fadeIn(1000);										
		} else {
			$('#achievehover').html('<br/><br/>');
		}
	}

	/* LOADING ACHIEVEMENTS */	
	function appendachievements(){
		for(var acmvt = 0; acmvt < achievements.length; acmvt++){
			var id = -1;
			for(var i = 0, len = achievements.length; i < len; i++) {
				if (achievements[i].achieveid === acmvt) {
					id = i;
					break;
				}
			}
			if (id != -1){
				if (achievements[id].achieved){
					unlocked_achievements++;
					var posx = -64 * (achievements[id].posx - 1);
					var posy = -64 * (achievements[id].posy - 1);
					var rank = achievements[id].rank;
				} else {
					var posx = 0;
					var posy = 0;
					var rank = 0;
				}
				var div =
					'<div class = "achieve' + rank + '" id = "'+id+'wrapper">'+
						'<div '+
							'class = "achievement"'+
							'id = "achieve'+ id +'"'+
							'style = "background-image: url(images/icons.png); background-position: ' + posx + 'px ' + posy + 'px; height: 64px; width: 64px;"'+
							'onmouseover = "achievehover('+ id +',true)"'+
						'></div>'+
					'</div>';
				$(div).hide().appendTo('#achievementslist').fadeIn(1000);
			}
		}
	}

	function append_upgrades(){
		for (var id = 0; id < upgrades.length; id++){
			if (upgrades[id].purchased){
				var posx = -64 * (upgrades[id].posx - 1);
				var posy = -64 * (upgrades[id].posy - 1);
				var rank = upgrades[id].rank;
				var div =
					'<div class="achieve' + rank + '">'+
						'<div '+
							'class="upgrade"'+
							'id = "upgrade'+id+'"'+
							'style = "background-image:url(images/icons.png); background-position:' + posx + 'px ' + posy + 'px;"'+
							'onmouseover = "upgradehover('+id+', true, true)"'+
						'></div>'+
					'</div>';
				$(div).hide().appendTo('#bought_upgrades_list').fadeIn(1000);
			}
		}
	}
	
	function append_available_upgrades(id){
		if (upgrades[id].unlocked && !upgrades[id].purchased && !upgrades[id].appended){
			var posx = -64 * (upgrades[id].posx - 1);
			var posy = -64 * (upgrades[id].posy - 1);
			var rank = upgrades[id].rank;
			var div =
				'<div class = "achieve' + rank + '" id = "upgrade_'+id+'_wrapper">'+
					'<div '+
						'class="upgrade"'+
						'id = "upgrade' + id + '"'+
						'style = "background-image:url(images/icons.png); background-position:' + posx + 'px ' + posy + 'px;"'+
						'onmouseover = "upgradehover('+id+', true, false)"'+
						'onclick = "buy_upgrade('+id+')"'+
					'></div>'+
				'</div>';
			$(div).appendTo('#upgrades_list').fadeIn(1000);
			upgrades[id].appended = true;
		}	
	}
		/* UPGRADES */
		var upgrades = [

			{"title"	: "Sandstorm",
			"desc"		: "Ups the efficiency of Stone Badges by <strong>+0.2</strong> base Spheal Smiles per second.",
			"cost"		: 80,
			"rank"		: 1, "posx" : 1, "posy" : 5,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},

			{"title"	: "Acupressure",
			"desc"		: "Ups the efficiency of Knuckle Badges by <strong>+1</strong> base Spheal Smiles per second.",
			"cost"		: 1100,
			"rank"		: 1, "posx" : 2, "posy" : 5,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},

			{"title"	: "Charge",
			"desc"		: "Ups the efficiency of Dynamo Badges by <strong>+3</strong> base Spheal Smiles per second.",
			"cost"		: 5000,
			"rank"		: 1, "posx" : 3, "posy" : 5,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},

			{"title"	: "Sunny Day",
			"desc"		: "Ups the efficiency of Heat Badges by <strong>+10</strong> base Spheal Smiles per second.",
			"cost"		: 12000,
			"rank"		: 1, "posx" : 4, "posy" : 5,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},

			{"title"	: "Howl",
			"desc"		: "Ups the efficiency of Balance Badges by <strong>+50</strong> base Spheal Smiles per second.",
			"cost"		: 50000,
			"rank"		: 1, "posx" : 5, "posy" : 5,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},

			{"title"	: "Roost",
			"desc"		: "Ups the efficiency of Feather Badges by <strong>+100</strong> base Spheal Smiles per second.",
			"cost"		: 90000,
			"rank"		: 1, "posx" : 6, "posy" : 5,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},

			{"title"	: "Meditate",
			"desc"		: "Ups the efficiency of Mind Badges by <strong>+400</strong> base Spheal Smiles per second.",
			"cost"		: 120000,
			"rank"		: 1, "posx" : 7, "posy" : 5,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},

			{"title"	: "Water Sport",
			"desc"		: "Ups the efficiency of Rain Badges by <strong>+600</strong> base Spheal Smiles per second.",
			"cost"		: 400000,
			"rank"		: 1, "posx" : 1, "posy" : 6,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},

			{"title"	: "Noland's Tips",
			"desc"		: "Ups the efficiency of Frontier Knowledge Symbols by <strong>+800</strong> base Spheal Smiles per second.",
			"cost"		: 3500000,
			"rank"		: 1, "posx" : 3, "posy" : 6,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Rock Polish",
			"desc"		: "<strong>Doubles</strong> the efficiency of Stone Badges.",
			"cost"		: 5000,
			"rank"		: 2, "posx" : 1, "posy" : 5,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Swords Dance",
			"desc"		: "<strong>Doubles</strong> the efficiency of Knuckle Badges.",
			"cost"		: 12000,
			"rank"		: 2, "posx" : 2, "posy" : 5,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Ion Deluge",
			"desc"		: "<strong>Doubles</strong> the efficiency of Dynamo Badges.",
			"cost"		: 90000,
			"rank"		: 2, "posx" : 3, "posy" : 5,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Will-o-Wisp",
			"desc"		: "<strong>Doubles</strong> the efficiency of Heat Badges.",
			"cost"		: 120000,
			"rank"		: 2, "posx" : 4, "posy" : 5,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Acupressure",
			"desc"		: "<strong>Doubles</strong> the efficiency of Balance Badges.",
			"cost"		: 175000,
			"rank"		: 2, "posx" : 5, "posy" : 5,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Tailwind",
			"desc"		: "<strong>Doubles</strong> the efficiency of Feather Badges.",
			"cost"		: 1200000,
			"rank"		: 2, "posx" : 6, "posy" : 5,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Calm Mind",
			"desc"		: "<strong>Doubles</strong> the efficiency of Mind Badges.",
			"cost"		: 17500000,
			"rank"		: 2, "posx" : 8, "posy" : 5,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Rain Dance",
			"desc"		: "<strong>Doubles</strong> the efficiency of Rain Badges.",
			"cost"		: 50000000,
			"rank"		: 2, "posx" : 2, "posy" : 6,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Noland's Knowledge",
			"desc"		: "<strong>Doubles</strong> the efficiency of Frontier Knowledge Symbols.",
			"cost"		: 75000000,
			"rank"		: 2, "posx" : 3, "posy" : 6,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Seven Seven Seven",
			"desc"		: "Ups the efficiency of Frontier Luck Symbols by <strong>+1,000</strong> base Spheal Smiles per second.",
			"cost"		: 77777777,
			"rank"		: 1, "posx" : 4, "posy" : 6,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Summon",
			"desc"		: "Ups the efficiency of Frontier Spirits Symbols by <strong>+1,400</strong> base Spheal Smiles per second.",
			"cost"		: 120000000,
			"rank"		: 1, "posx" : 5, "posy" : 6,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Badge of Honour",
			"desc"		: "Ups the efficiency of Frontier Brave Symbols by <strong>+1,900</strong> base Spheal Smiles per second.",
			"cost"		: 120000000,
			"rank"		: 1, "posx" : 6, "posy" : 6,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Fashonista",
			"desc"		: "Ups the efficiency of Frontier Tactics Symbols by <strong>+2,400</strong> base Spheal Smiles per second.",
			"cost"		: 180000000,
			"rank"		: 1, "posx" : 7, "posy" : 6,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Guts",
			"desc"		: "Ups the efficiency of Frontier Guts Symbols by <strong>+2,900</strong> base Spheal Smiles per second.",
			"cost"		: 240000000,
			"rank"		: 1, "posx" : 8, "posy" : 6,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Tycoon's Take",
			"desc"		: "Ups the efficiency of Frontier Ability Symbols by <strong>+3,200</strong> base Spheal Smiles per second.",
			"cost"		: 310000000,
			"rank"		: 1, "posx" : 1, "posy" : 7,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Seviper Bite",
			"desc"		: "<strong>Doubles</strong> the efficiency of Frontier Luck Symbols.",
			"cost"		: 477777777,
			"rank"		: 2, "posx" : 4, "posy" : 6,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Necromancy",
			"desc"		: "<strong>Doubles</strong> the efficiency of Frontier Spirits Symbols.",
			"cost"		: 500000000,
			"rank"		: 2, "posx" : 5, "posy" : 6,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Willpower",
			"desc"		: "<strong>Doubles</strong> the efficiency of Frontier Brave Symbols.",
			"cost"		: 640000000,
			"rank"		: 2, "posx" : 6, "posy" : 6,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Tucker's Technique",
			"desc"		: "<strong>Doubles</strong> the efficiency of Frontier Tactics Symbols.",
			"cost"		: 710000000,
			"rank"		: 2, "posx" : 7, "posy" : 6,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Extra Muscle",
			"desc"		: "<strong>Doubles</strong> the efficiency of Frontier Guts Symbols.",
			"cost"		: 1000000000,
			"rank"		: 2, "posx" : 8, "posy" : 6,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false},
			
			{"title"	: "Anabel Assist",
			"desc"		: "<strong>Doubles</strong> the efficiency of Frontier Ability Symbols.",
			"cost"		: 1045000000,
			"rank"		: 2, "posx" : 1, "posy" : 7,
			"unlocked"	: false, "purchased" : false, "appended" : false, "effect" : false}
			
		]	

	/* PUT UPGRADES INTO EFFECT */
	function upgrade_effects(){
		for (var id = 0; id < upgrades.length; id++){
			if (upgrades[id].purchased){
				if(id == 0 && !upgrades[id].effect){autoclickers[0].cps += 0.2;}
				if(id == 1 && !upgrades[id].effect){autoclickers[1].cps += 1;}
				if(id == 2 && !upgrades[id].effect){autoclickers[2].cps += 3;}
				if(id == 3 && !upgrades[id].effect){autoclickers[3].cps += 10;}
				if(id == 4 && !upgrades[id].effect){autoclickers[4].cps += 50;}
				if(id == 5 && !upgrades[id].effect){autoclickers[5].cps += 100;}
				if(id == 6 && !upgrades[id].effect){autoclickers[6].cps += 400;}
				if(id == 7 && !upgrades[id].effect){autoclickers[7].cps += 600;}
				if(id == 8 && !upgrades[id].effect){autoclickers[8].cps += 800;}
				if(id == 18 && !upgrades[id].effect){autoclickers[9].cps += 1000;}
				if(id == 19 && !upgrades[id].effect){autoclickers[10].cps += 1400;}
				if(id == 20 && !upgrades[id].effect){autoclickers[11].cps += 1900;}
				if(id == 21 && !upgrades[id].effect){autoclickers[12].cps += 2400;}
				if(id == 22 && !upgrades[id].effect){autoclickers[13].cps += 2900;}
				if(id == 23 && !upgrades[id].effect){autoclickers[14].cps += 3200;}
				if(id == 9 && !upgrades[id].effect){autoclickers[0].cps *= 2;}
				if(id == 10 && !upgrades[id].effect){autoclickers[1].cps *= 2;}
				if(id == 11 && !upgrades[id].effect){autoclickers[2].cps *= 2;}
				if(id == 12 && !upgrades[id].effect){autoclickers[3].cps *= 2;}
				if(id == 13 && !upgrades[id].effect){autoclickers[4].cps *= 2;}
				if(id == 14 && !upgrades[id].effect){autoclickers[5].cps *= 2;}
				if(id == 15 && !upgrades[id].effect){autoclickers[6].cps *= 2;}
				if(id == 16 && !upgrades[id].effect){autoclickers[7].cps *= 2;}
				if(id == 17 && !upgrades[id].effect){autoclickers[8].cps *= 2;}
				if(id == 24 && !upgrades[id].effect){autoclickers[9].cps *= 2;}
				if(id == 25 && !upgrades[id].effect){autoclickers[10].cps *= 2;}
				if(id == 26 && !upgrades[id].effect){autoclickers[11].cps *= 2;}
				if(id == 27 && !upgrades[id].effect){autoclickers[12].cps *= 2;}
				if(id == 28 && !upgrades[id].effect){autoclickers[13].cps *= 2;}
				if(id == 29 && !upgrades[id].effect){autoclickers[14].cps *= 2;}
				upgrades[id].effect = true;
			}
		}
	}

	/* CHECKING UPGRADES */
	function check_upgrades(){
		for (var id = 0; id < upgrades.length; id++){
				if (id == 0 && autoclickers[0].quantity >= 1){upgrades[id].unlocked = true;}
				if (id == 1 && autoclickers[1].quantity >= 1){upgrades[id].unlocked = true;}
				if (id == 2 && autoclickers[2].quantity >= 1){upgrades[id].unlocked = true;}
				if (id == 3 && autoclickers[3].quantity >= 1){upgrades[id].unlocked = true;}
				if (id == 4 && autoclickers[4].quantity >= 1){upgrades[id].unlocked = true;}
				if (id == 5 && autoclickers[5].quantity >= 1){upgrades[id].unlocked = true;}
				if (id == 6 && autoclickers[6].quantity >= 1){upgrades[id].unlocked = true;}
				if (id == 7 && autoclickers[7].quantity >= 1){upgrades[id].unlocked = true;}
				if (id == 8 && autoclickers[8].quantity >= 1){upgrades[id].unlocked = true;}
				if (id == 18 && autoclickers[9].quantity >= 1){upgrades[id].unlocked = true;}
				if (id == 19 && autoclickers[10].quantity >= 1){upgrades[id].unlocked = true;}
				if (id == 20 && autoclickers[11].quantity >= 1){upgrades[id].unlocked = true;}
				if (id == 21 && autoclickers[12].quantity >= 1){upgrades[id].unlocked = true;}
				if (id == 22 && autoclickers[13].quantity >= 1){upgrades[id].unlocked = true;}
				if (id == 23 && autoclickers[14].quantity >= 1){upgrades[id].unlocked = true;}
				if (id == 9 && autoclickers[0].quantity >= 20){upgrades[id].unlocked = true;}
				if (id == 10 && autoclickers[1].quantity >= 20){upgrades[id].unlocked = true;}
				if (id == 11 && autoclickers[2].quantity >= 20){upgrades[id].unlocked = true;}
				if (id == 12 && autoclickers[3].quantity >= 20){upgrades[id].unlocked = true;}
				if (id == 13 && autoclickers[4].quantity >= 20){upgrades[id].unlocked = true;}
				if (id == 14 && autoclickers[5].quantity >= 20){upgrades[id].unlocked = true;}
				if (id == 15 && autoclickers[6].quantity >= 20){upgrades[id].unlocked = true;}
				if (id == 16 && autoclickers[7].quantity >= 20){upgrades[id].unlocked = true;}
				if (id == 17 && autoclickers[8].quantity >= 20){upgrades[id].unlocked = true;}
				if (id == 24 && autoclickers[9].quantity >= 20){upgrades[id].unlocked = true;}
				if (id == 25 && autoclickers[10].quantity >= 20){upgrades[id].unlocked = true;}
				if (id == 26 && autoclickers[11].quantity >= 20){upgrades[id].unlocked = true;}
				if (id == 27 && autoclickers[12].quantity >= 20){upgrades[id].unlocked = true;}
				if (id == 28 && autoclickers[13].quantity >= 20){upgrades[id].unlocked = true;}
				if (id == 29 && autoclickers[14].quantity >= 20){upgrades[id].unlocked = true;}
				append_available_upgrades(id);
		}
	}

	/* HOVERING OVER / OUT OF AN UPGRADE */
	function upgradehover(id,mousein,purchased){
		if(mousein){
			if(!purchased){
				var div = '<span id="upgrade_desc"><i>' + cnItem(upgrades[id].title) + '</i> - <i>花费:</i> '+ numberWithCommas(upgrades[id].cost) +'<br/>'+ upgrades[id].desc +'</span>';
				$('#upgrade_hover').html(div);
			} else {
				var div = '<span id="upgrade_desc"><i>' + cnItem(upgrades[id].title) + '</i><br/>'+ upgrades[id].desc +'</span>';
				$('#bought_upgrade_hover').html(div);
			}
			$(div).hide().fadeIn(1000);										
		} else {
			if(!purchased){$('#upgrade_hover').html('<br/><br/>');}else{$('#bought_upgrade_hover').html('<br/><br/>');}
		}
	}

	/* BUYING AN UPGRADE */
	function buy_upgrade(id){
		if(clicks >= upgrades[id].cost && !upgrades[id].purchased){
			clicks -= upgrades[id].cost;
			upgrades[id].purchased = true;
			$('#upgrade_'+id+'_wrapper').fadeOut(100);
			var posx = -64 * (upgrades[id].posx - 1);
			var posy = -64 * (upgrades[id].posy - 1);
			var rank = upgrades[id].rank;
			var div =
				'<div class="achieve' + rank + '" id="upgrade_'+id+'_wrapper">'+
					'<div class="upgrade"'+
						'id = "upgrade'+id+'"'+
						'style = "background-image:url(images/icons.png); background-position:' + posx + 'px ' + posy + 'px;"'+
						'onmouseover = "upgradehover('+id+', true, true)"'+
					'</div>'+
				'</div>';
			$(div).hide().appendTo('#bought_upgrades_list').fadeIn(1000);
			popUp('已购买 ' + cnItem(upgrades[id].title) + ' 升级!');
			autoSave();
		}
		upgrade_effects();
		recalccps();
	}

	function recalccps(){
		cps = 0;
		for (var i = 0; i < autoclickers.length; i++){
			cps += autoclickers[i].quantity * autoclickers[i].cps;
		}
		for (var clicker = 0; clicker < autoclickers.length; clicker++){
			$('#cps' + clicker).html('+' + numberWithCommas(roundTo(2,autoclickers[clicker].cps)) + ' 每秒');
		}
		$('.cps').html(numberWithCommas(roundTo(1,cps)));
	}

	var pkmnmodifier = 0;
	function calcmodifier(){
		if (pkmn.length > 2){
			pkmnmodifier = 0;
			for (var i = 1; i < pkmn.length; i++){
				if (pkmn[i].species == 'Spheal'){	pkmnmodifier += 0.1;}
				if (pkmn[i].species == 'Sealeo'){	pkmnmodifier += 0.2;}
				if (pkmn[i].species == 'Walrein'){	pkmnmodifier += 0.4;}
				if (pkmn[i].shiny){					pkmnmodifier *= 1.25;}
				if (pkmn[i].level >= 25 && pkmn[i].level < 50){	pkmnmodifier += 0.1;}
				if (pkmn[i].level >= 50 && pkmn[i].level < 99){	pkmnmodifier += 0.5;}
				if (pkmn[i].level == 100){			pkmnmodifier += 0.7;}
			}
		}
		recalcstats();
	}
/* MISC. FUNCTIONS */
	/* TITLE UPDATE */
	window.setInterval(function(){updateTitle();},2000)
	function updateTitle(){document.title = numberWithCommas(roundTo(3,clicks)) + " Spheal Smiles - Spheal Clicker";}

	/* AUTOSAVING */
	window.setInterval(function(){autoSave();},120000)
	
	/* REMOVING ELEMENT */
	function remove(id){
		return (elem=document.getElementById(id)).parentNode.removeChild(elem);
	}

	/* CLEARING SAVE */
	function confirm_clear(n){
		if (n == 0){$('#clear_game').html('<button onclick="confirm_clear(1)">Are you sure?</button>');}
		if (n == 1){$('#clear_game').html('<button onclick="gameClear()">Really?</button>');}
	}
		
	/* RECALCULATING STATS */
	function recalcstats(){
		$('.achivements_data').html(numberWithCommas(unlocked_achievements) + ' / ' + numberWithCommas(achievements.length));
		$('.achivements_percent').html(Math.round(unlocked_achievements / achievements.length * 100));
		$('.modifier').html(numberWithCommas(roundTo(2,pkmnmodifier)));
		$('.pokemon_total').html(numberWithCommas(pkmn.length - 1));
		$('.clicked_swim_spheal').html(numberWithCommas(clicked_swim_spheal));
		$('.clicks').html(numberWithCommas(clicks.toFixed(2)));
		$('.clicks_all_time').html(numberWithCommas(clicks_all_time.toFixed(2)));
		$('.cps').html(numberWithCommas(roundTo(1,cps)));
		var upgrades_bought = 0;
		for (var i = 0; i < upgrades.length; i++){
			if(upgrades[i].purchased){upgrades_bought++;}
		}
		$('.upgrades_bought').html(numberWithCommas(upgrades_bought) + ' / ' + numberWithCommas(upgrades.length));
		$('.upgrades_percent').html(Math.round(upgrades_bought / upgrades.length * 100));
		check_upgrades();
	}

	function changeleft(showid) {
		$('.container').hide();
		$('#' + showid).show();
	}

	function numberWithCommas(x) {
	    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
	}

	function popUp(string){
		if(notifs){
			var status = "<span><br/>" + string + "</span>";
			$(status).attr('id','status').prependTo(".status").fadeIn(1000).delay(5000).fadeOut(1000, function(){$(this).remove();});
		}
	}
	
	function roundTo(dp,num) {    
		return +(Math.round(num + "e+"+dp)  + "e-"+dp);
	}

	function toggleautosave(obj) {
		if(autosave){
			obj.innerHTML = '已禁用。';
			popUp('自动保存已禁用。');
			autosave = false;
		} else {
			obj.innerHTML = '已启用。';
			popUp('自动保存已启用。');
			autosave = true;
		}
	}
	
	function togglenotifs(obj) {
		if(notifs){
			obj.innerHTML = '已禁用。';
			popUp('通知已被禁用。');
			notifs = false;
		} else {
			notifs = true;
			obj.innerHTML = '已启用。';
			popUp('通知已启用。');
		}
	}

	function dirtycheater() {
		var cheatmode = 
			'<br/>'+
			'<button onclick="clicked(999999999999)">gib moni</button><br/>'+
			'<button onclick="capturepkmn(98,true)">Capture Lv 98 Shiny Spheal</button>'+
			'<button onclick="spawnswim()">Spawn Swimming Spheal</button>'+
			'<button onclick="spawnpkmn()">Spawn Wild Spheal</button><br/>'+
			'<button onclick="randomitem()">Spawn Random Item</button>'+
			'<button onclick="spawnitem(0)">Spawn Rare Candy</button>'+
			'<button onclick="spawnitem(1)">Spawn Amulet Coin</button>'+
			'<button onclick="spawnitem(6)">Spawn Shiny Charm</button><br/>'+
			'<button onclick="spawnitem(2)">Spawn Poké Ball</button>'+
			'<button onclick="spawnitem(3)">Spawn Great Ball</button>'+
			'<button onclick="spawnitem(4)">Spawn Ultra Ball</button>'+
			'<button onclick="spawnitem(5)">Spawn Master Ball</button>'
		$(cheatmode).hide().appendTo('.right_wrapper').fadeIn(1000);
	}